/*
 * uart1.c
 *
 *  Created on: 13-11-2012
 *      Author: Skeith
 */

#include "uart1.h"

void Uart1SetPinsel(void)
{
	tU32 val = 1 << 16 | 1 << 18;
	tU32 maskedVal1;

	maskedVal1 = PINSEL0 & ~UART1_PINSEL_MASK;

	PINSEL0 = maskedVal1 | val;

}

void Uart1Init()
{
	tU16 divFactor;
	volatile tU32 dummy;

	//Uart1SetPinsel();

	U1IER = 0x00; //wylaczenie przerwan

	U1IER = 0x00;                        //disable all uart interrupts
	dummy = U1IIR;                       //clear all pending interrupts
	dummy = U1RBR;                       //clear receive register
	dummy = U1LSR;                       //clear line status register

	//tryb pracy - 9600, bez bitu parzystosci, 1 bit stopu

	divFactor = ((CRYSTAL_FREQUENCY * PLL_FACTOR) / VPBDIV_FACTOR) / (16 * 9600);

	U1LCR = 1 << 7;
	U1DLL = (tU8)divFactor;
	U1DLM = (tU8)(divFactor >> 8);

	U1LCR = 1 | 1 << 1 ; // zerowanie dlab, 9600, no parity, 1 bit stopu
	U1FCR = 0x01;
	Uart1ClrFifo();
	Sleep(100);

	lcdPuts("init");

}

void Uart1SendChar(tU8 ch)
{
	/*if(ch == '\n')
	{
		while(!(U1LSR & 0x20));
		U1THR = '\r';
	}*/

	while(!(U1LSR & 0x20));
	U1THR = ch;
}

void Uart1SendBuffer(tU8* pBuff, tU16 count)
{
  while (count--)
    Uart1SendChar(*pBuff++);
}

tU8 Uart1GetChar()
{
	while(!(U1LSR & 0x01));
	return U1RBR;
}

void Uart1GetBuffer(tU8* readBuffer, tU16 count)
{
	while(count--)
		*readBuffer++ = Uart1GetChar();
}

tBool Uart1ReadCheck()
{
	if(U1LSR & 0x01)
		return TRUE;

	return FALSE;
}

void Uart1ClrFifo()
{
	U1FCR = U1FCR & 0x06;
	Sleep(100);
}

/*tBool XmSendData(tU8* data, tU32 bytes)
{
	tU8 failedRetry = 0;
	tU16 finalPacketSize;
	tBool initalized = FALSE;
	int i;
	//char pack[10];
	
	packetNumber = 1;
	stopPoint = 0;
	eof = FALSE;

	//Odczytanie pierwszego znaku z bufora
	for (i = 0; i <= 6; i++)
	{
		//if(Uart1ReadCheck())
		{
			initSign = Uart1GetChar();

			if(initSign == c)
			{
				useCRC = TRUE;
				finalPacketSize = sizeof(dataToSend);
				initalized = TRUE;
				Uart1ClrFifo();
				break;
			}
			else if (initSign == nak)
			{
				useCRC = FALSE;
				finalPacketSize = sizeof(dataToSend) - 1;
				initalized = TRUE;
				Uart1ClrFifo();
				break;
			}
		}
	}



	if(initalized == FALSE)
	{
		lcdPuts("FAIL");
		return FALSE;
	}


	//Wysylanie pliku
	//lcdClrscr();

	while(failedRetry <= 6)
	{
		XmMakePacket(data);

		Uart1SendBuffer(&dataToSend, finalPacketSize);
		initSign = Uart1GetChar();

		//intToChar(packetNumber, pack);


		switch(initSign)
		{
		case ack:
			{
				failedRetry = 0;
				lcdPuts("ack\n");
				break;
			}
		case nak:
			{
				lcdPuts("nak\n");
				do
				{
					//Powtarzanie transmisji pakietu
					Uart1SendBuffer(&dataToSend, finalPacketSize);
					initSign = Uart1GetChar();
					lcdPuts("nak2\n");
					failedRetry++;

				}
				while(initSign != ack && failedRetry <= 6);
				break;
			}
		case can:
			{
				lcdPuts("can\n");
				return FALSE;
				break;
			}
		default:
			{
				do
				{
					Uart1SendBuffer(&dataToSend, finalPacketSize);
					initSign = Uart1GetChar();
					failedRetry++;

				}
				while(initSign != ack && failedRetry <= 6);
				break;
			}
		}

		if(eof == TRUE)
		{
			break;
		}
	}

	if(failedRetry >= 6)
	{
		initSign = can;
		Uart1SendChar(initSign);
		return FALSE;
	}


	do
	{
		initSign = eot;
		Uart1SendChar(initSign);
		initSign = Uart1GetChar();
	}
	while(initSign != ack);

	return TRUE;
}

void XmMakePacket(tU8* data)
{
	tU32 counter;
	int i;
	XmResetPacket();

	dataToSend.startOfHeader = soh;

	if(packetNumber > 255)
	{
		packetNumber = 1;
	}


	dataToSend.packetNum = packetNumber;
	dataToSend.packetNumCompl = 255 - packetNumber;
	packetNumber++;
	counter = 0;

	//tBool readResult = ReadFile((HANDLE)hFile, &dataToSend.fileData, 128, &counter , NULL);

	//Przekopiowanie danyc do tablicy
	for(i = stopPoint; i < stopPoint + 128; i++)
	{
		if(data[i] == '\0')
		{
			eof = TRUE;
			break;
		}

		dataToSend.fileData[i] = data[i];
		counter++;
	}

	stopPoint += counter;

	if(counter < 128)
	{
		for(i = counter; i < 128; i++)
		{
			dataToSend.fileData[i] = 0x1A;
		}
	}

	if(useCRC)
	{
		tU16 tmp = XmCRC();
		dataToSend.checkSum1 = tmp >> 8;
		dataToSend.checkSum2 = tmp;
	}
	else
	{
		//dataToSend.checkSum1 = 1;
		dataToSend.checkSum1 = XmCheckSum();
	}

}

void XmResetPacket()
{
	int i;
	dataToSend.startOfHeader = 0;
	dataToSend.packetNum = 0;
	dataToSend.packetNumCompl = 0;
	dataToSend.checkSum1 = 0;
	dataToSend.checkSum2 = 0;

	for(i = 0; i < 128; i++)
		dataToSend.fileData[i] = 0;
}

tU16 XmCRC()
{
	crc	= 0x0000;
	int i;
	int j;
	for ( i = 0; i < sizeof(dataToSend.fileData); i++ )
	{
		crc = crc ^ (int)dataToSend.fileData[i] << 8;
		for (j = 0; j < 8; ++j)
		{
			if (crc & 0x8000)
			{
				crc = crc << 1 ^ 0x1021;
			}
			else
			{
				crc = crc << 1;
			}
		}
	}
	return (crc	& 0xFFFF);
}

tU8 XmCheckSum()
{
	tU8 checksum = 0;
	char val[8];
	int i;

	for(i = 0; i < sizeof(dataToSend.fileData); i++)
	{
		checksum += (tU8)dataToSend.fileData[i];
	}

	intToChar(checksum, val);
	lcdPuts(val);

	return checksum;
}
*/
